package com.z.util.convert;

import java.io.File;
import java.util.UUID;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.OfficeManager;

import com.z.config.ConfigReader;
import com.z.dao.UploadFileDao;
import com.z.entity.UploadFile;

public class Convert implements Runnable {
	public static final String SWF_TOOLS_PATH="pdf2swf.exe";
	
	public static final String DOCUMENT_DOC="doc";
	public static final String DOCUMENT_DOCX="docx";
	public static final String DOCUMENT_RTF="rtf";
	public static final String DOCUMENT_TXT="txt";
	public static final String DOCUMENT_XLS="xls";
	public static final String DOCUMENT_XLSX="xlsx";
	public static final String DOCUMENT_PPT="ppt";
	public static final String DOCUMENT_PPTX="pptx";
	public static final String DOCUMENT_PNG="png";
	public static final String DOCUMENT_ICO="ico";
	public static final String DOCUMENT_BMP="bmp";
	public static final String DOCUMENT_JPG="jpg";
	public static final String DOCUMENT_PDF="pdf";
	public static final String DOCUMENT_TIF="tif";
	public static final String DOCUMENT_TIFF="tiff";

	public static final String TEMP_FOLDER="temp";
	public static final String SWF_FOLDER="swf";
	
	private UploadFile uploadFile;
	private OfficeManager officeManager;
	
	public Convert(UploadFile uploadFile,OfficeManager officeManager){
		this.uploadFile=uploadFile;
		this.officeManager=officeManager;
	}
	
	public void pdf2swf(String pdfPath){
		try {
			String root = new File(this.getClass().getClassLoader()
			        .getResource("/").toURI()).getPath();
			System.out.println(root+"/"+SWF_TOOLS_PATH);
			String swfPath=ConfigReader.getInstance().makeFolder(SWF_FOLDER)+UUID.randomUUID().toString()+".swf";
			String cmd = root+"/"+SWF_TOOLS_PATH + " "+pdfPath+" -o "+swfPath+" -f -T 9";
			System.out.println("#2 BEGIN...");
			Runtime.getRuntime().exec(cmd);
			System.out.println("#2 END");
			UploadFileDao.getInstace().finish(uploadFile.getId(),ConfigReader.getInstance().removeRoot(swfPath));
		} catch (Exception e) {
			System.out.println("#2 ERR-"+e.toString());
		}
	}
	
	
	@Override
	public void run() {
		OfficeDocumentConverter officeDocumentConverter = new OfficeDocumentConverter(officeManager);
		String name=uploadFile.getName().toLowerCase();
		if(name.endsWith(DOCUMENT_DOC)||name.endsWith(DOCUMENT_DOCX)
				||name.endsWith(DOCUMENT_RTF)||name.endsWith(DOCUMENT_TXT)
				||name.endsWith(DOCUMENT_XLS)||name.endsWith(DOCUMENT_XLSX)){
			String pdfPath=ConfigReader.getInstance().makeFolder(TEMP_FOLDER)+UUID.randomUUID().toString()+".pdf";
			try{
				System.out.println("#0 BEGIN...");
				officeDocumentConverter.convert(new File(ConfigReader.getInstance().getAttachRoot()+uploadFile.getPath()), new File(pdfPath));
				System.out.println("#0 END");
				pdf2swf(pdfPath);
			}catch(Exception e){
				System.out.println("#0 ERR-"+e.toString());
			}
		}else if(name.endsWith(DOCUMENT_PPT)||name.endsWith(DOCUMENT_PPTX)
				||name.endsWith(DOCUMENT_PDF)
				||name.endsWith(DOCUMENT_BMP)
				||name.endsWith(DOCUMENT_ICO)
				||name.endsWith(DOCUMENT_PNG)
				||name.endsWith(DOCUMENT_TIF)
				||name.endsWith(DOCUMENT_TIFF)
				||name.endsWith(DOCUMENT_JPG)){
			String swfPath=ConfigReader.getInstance().makeFolder(SWF_FOLDER)+UUID.randomUUID().toString()+".swf";
			try{
				System.out.println("#1 BEGIN...");
				officeDocumentConverter.convert(new File(ConfigReader.getInstance().getAttachRoot()+uploadFile.getPath()), new File(swfPath));
				System.out.println("#1 END");
				UploadFileDao.getInstace().finish(uploadFile.getId(),ConfigReader.getInstance().removeRoot(swfPath));
			}catch(Exception e){
				System.out.println("#1 ERR-"+e.toString());
			}
		}
	}
	
}
